package com.lul15834.orderfood.activity.manager;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;


import com.lul15834.orderfood.R;
import com.lul15834.orderfood.dao.AdminDao;
import com.lul15834.orderfood.until.FileImgUntil;

import java.util.UUID;

public class RegisterManager extends AppCompatActivity {
    private ActivityResultLauncher<String> getContentLauncher;
    Uri uri;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register_manager);
        //实现返回功能
        Toolbar toolbar= findViewById(R.id.register_back);
        setSupportActionBar(toolbar);
        //返回两种方式，采用跳转和管理
        toolbar.setNavigationOnClickListener(v -> onBackPressed());
        //初始化文件选择器
        ImageView imgText=findViewById(R.id.register_man_image);
         getContentLauncher = registerForActivityResult(new ActivityResultContracts.GetContent(), new ActivityResultCallback<Uri>() {
            @Override
            public void onActivityResult(Uri result) {
                if (result != null) {
                    imgText.setImageURI(result);
                    uri=result;
                } else {
                    Toast.makeText(RegisterManager.this, "请输入店铺类型", Toast.LENGTH_SHORT).show();
                }
            }
        });
        //获取默认头像
        Drawable defaultDrawable= ContextCompat.getDrawable(this,R.drawable.upimg);
        //ID
        EditText idText=findViewById(R.id.register_man_id);
        EditText pwdText=findViewById(R.id.register_man_pwd);
        EditText nameText=findViewById(R.id.register_man_name);
        EditText desText=findViewById(R.id.register_man_des);
        EditText typeText=findViewById(R.id.register_man_type);
        Button btn=findViewById(R.id.register_btn);
        btn.setOnClickListener(new View.OnClickListener() {
            private ActivityResultLauncher<String> getContentLauncher;
            @Override
            public void onClick(View v) {
                //判断是否选择头像
                Drawable drawable= imgText.getDrawable();//获取当前标签的图片
                String id=idText.getText().toString();//Id变化
                String pwd=pwdText.getText().toString();//Id变化
                String name=nameText.getText().toString();//Id变化
                String des=desText.getText().toString();//Id变化
                String type=typeText.getText().toString();//Id变化
                if (drawable instanceof BitmapDrawable){//判断是不是图片类型
                    //向数据库中插入数据
                    Bitmap bitmap =((BitmapDrawable) drawable).getBitmap();//获取图片的二进制文件
                    Bitmap bitmapDef =((BitmapDrawable) defaultDrawable).getBitmap();//获取图片的二进制文件
                    //检查Bitmap是否与默认鱼片相同
                    if (bitmap.sameAs(bitmapDef)){//判断是不是默认图片
                        Toast.makeText(RegisterManager.this,"请点击图片进行添加头像",Toast.LENGTH_SHORT).show();
                    }else if (id.isEmpty()){
                            Toast.makeText(RegisterManager.this,"请输入店铺账号",Toast.LENGTH_SHORT).show();
                    }else if (pwd.isEmpty()){
                        Toast.makeText(RegisterManager.this,"请输入店铺密码",Toast.LENGTH_SHORT).show();
                    }else if (name.isEmpty()){
                        Toast.makeText(RegisterManager.this,"请输入店铺名称",Toast.LENGTH_SHORT).show();
                    }else if (des.isEmpty()){
                        Toast.makeText(RegisterManager.this,"请输入店铺描述",Toast.LENGTH_SHORT).show();
                    }else if (type.isEmpty()){
                        Toast.makeText(RegisterManager.this,"请输入店铺类型",Toast.LENGTH_SHORT).show();
                    }else {
                        //可以向数据库进行保存数据
                        //bitmap 我需要将他保存到另一个文件夹当中，并且把保存路径输入进去
                        String path=FileImgUntil.getImgName();//图片存储路径名字
                        FileImgUntil.saveImageBitmapToFileImg(uri,RegisterManager.this,path);//保存图片
                        int a=AdminDao.saveBusinessUser(id,pwd,name,des,type,path);
                        if (a==1){
                            Toast.makeText(RegisterManager.this,"注册商家成功",Toast.LENGTH_SHORT).show();
                        }else {
                            Toast.makeText(RegisterManager.this,"注册商家失败，账号冲突",Toast.LENGTH_SHORT).show();
                        }
                    }
                }
            }
        });
        //实现选择图片
        imgText.setOnClickListener(new View.OnClickListener() {//实现点击头像上传图片
            @Override
            public void onClick(View v) {
                //点开文件选择器
                openGallery(v);
            }
        });

    }

    /**
     * 打开文件选择器
     * @param v
     */

    private void openGallery(View v){
        getContentLauncher.launch("image/*");
    }
}